# -*- coding: utf-8 -*-
import traceback

from    pymongo     import Connection

server1 = 'localhost'
server2 = 'localhost'
port1 = 27017
port2 = 27017
conn1    = Connection(server1, port1)
db1      = conn1['my_database']
conn2    = Connection(server2, port2)
db2      = conn2['mobile9']

def copyCollection(name):
    try:
        collection1 = db1[name]
        collection2 = db2[name]
        result      = collection1.find({}) 
        if result!=None:
            for doc in result:
                check = collection2.find_one({'_id': doc['_id']})
                if check==None:
                    collection2.save(doc)
    except:
        traceback.print_exc()

if __name__ == '__main__':
    try:
        copyCollection('category')
        copyCollection('backend_user')
        print 'Finished.'
    except:
        traceback.print_exc()
